package cn.vivi.util;

import java.io.IOException;

import javax.servlet.ServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtils {

	// 序列化
	public static String parse(Object obj) {
		try {
			return new ObjectMapper().writeValueAsString(obj);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			return null;
		}
	}

	// 序列化
	public static void toJson(ServletResponse response, Object obj) {
		try {
			new ObjectMapper().writeValue(response.getOutputStream(), obj);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 反序列化
	public static Object toObject(String json, Class<?> clazz) {
		try {
			return new ObjectMapper().readValue(json, clazz);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			return null;
		}
	}
}
